package com.yxinsur.imc.web.controller.business;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.yxinsur.imc.business.excel.RecnTemplate;
import com.yxinsur.imc.business.excel.RecnTemplateExport;
import com.yxinsur.imc.business.form.RecnExpenseSearchForm;
import com.yxinsur.imc.business.form.RecnSearchForm;
import com.yxinsur.imc.business.service.IImcRecnExpenseService;
import com.yxinsur.imc.business.service.IImcRecnService;
import com.yxinsur.imc.business.vo.RecnDetailsCheckVo;
import com.yxinsur.imc.business.vo.RecnVo;
import com.yxinsur.imc.common.annotation.Log;
import com.yxinsur.imc.common.core.controller.BaseController;
import com.yxinsur.imc.common.core.domain.AjaxResult;
import com.yxinsur.imc.common.core.page.TableDataInfo;
import com.yxinsur.imc.common.enums.BusinessType;
import com.yxinsur.imc.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 首年手续费管理 Controller
 *
 * @author imc
 * @date 2021/5/12 11:04
 */
@Api(value = "首年手续费管理模块接口服务", tags = {"首年手续费管理模块接口服务"})
@RestController
@RequestMapping("/business/recn")
public class ImcRecnController extends BaseController {
    @Autowired
    private IImcRecnService imcRecnService;
    @Autowired
    private IImcRecnExpenseService imcRecnExpenseService;

    /**
     * 根据{选择月度}{供应商ID}查询协议
     *
     * @param searchForm RecnExpenseSearchForm
     * @return 协议
     */
    @ApiOperation("根据{选择月度}{供应商ID}查询协议")
    @GetMapping("/expense/contract")
    public AjaxResult listContract(RecnExpenseSearchForm searchForm) {
        String settleMonth = searchForm.getSettleMonth();
        Long supplierId = searchForm.getSupplierId();
        if (StrUtil.isBlank(settleMonth) || ObjectUtil.isEmpty(supplierId)) {
            return AjaxResult.error("选择月度、供应商不能为空");
        }
        return AjaxResult.success(imcRecnExpenseService.listContractBySettleMonthAndSupplierId(settleMonth, supplierId));
    }

    /**
     * 根据协议ID查询协议费用科目
     *
     * @param searchForm RecnExpenseSearchForm
     * @return 协议费用科目
     */
    @ApiOperation("根据协议ID查询协议费用科目")
    @GetMapping("/expense")
    public AjaxResult listImcContractExpenseByContractIds(RecnExpenseSearchForm searchForm) {
        List<Long> contractIdList = searchForm.getContractIdList();
        if (CollUtil.isEmpty(contractIdList)) {
            return AjaxResult.error("协议id不能为空");
        }
        return AjaxResult.success(imcRecnExpenseService.listImcContractExpenseByContractIds(contractIdList, searchForm.getExpenseType()));
    }

    /**
     * 根据主表ID查询协议费用科目
     *
     * @param searchForm RecnExpenseSearchForm
     * @return 协议费用科目
     */
    @ApiOperation("根据主表ID查询协议费用科目")
    @GetMapping("/expense/list/{recnId}")
    public TableDataInfo listImcContractExpenseByRecnId(RecnExpenseSearchForm searchForm) {
        Long recnId = searchForm.getRecnId();
        startPage();
        return getDataTable(imcRecnExpenseService.listImcContractExpenseByRecnId(recnId));
    }

    /**
     * 生成手续费对账单
     *
     * @param searchForm RecnExpenseSearchForm
     * @return AjaxResult
     */
    @ApiOperation("生成手续费对账单")
    @Log(title = "生成手续费对账单", businessType = BusinessType.INSERT)
    @PostMapping("/create")
    public AjaxResult create(@RequestBody RecnExpenseSearchForm searchForm) {
        return imcRecnService.createImcRecn(searchForm.getSettleMonth(), searchForm.getSupplierId(), searchForm.getContractIdList(), searchForm.getContractExpenseIdList());
    }

    /**
     * 导入模板
     *
     * @return AjaxResult
     */
    @ApiOperation(value = "导入模板", produces = "application/octet-stream")
    @GetMapping("/export/template")
    public AjaxResult importTemplate() {
        ExcelUtil<RecnTemplateExport> util = new ExcelUtil<>(RecnTemplateExport.class);
        return util.importTemplateExcel("首年供应商手续费明细");
    }

    /**
     * 导入供应商数据并核对
     *
     * @param recnId 主表ID
     * @return AjaxResult
     */
    @ApiOperation(value = "导入供应商数据并核对")
    @PostMapping("/import/{recnId}")
    public AjaxResult importAndCheck(MultipartFile file, @PathVariable("recnId") Long recnId) throws Exception {
        return this.imcRecnService.importAndCheck(file, recnId);
    }

    /**
     * 查询手续费对账单列表
     *
     * @param searchForm RecnSearchForm
     * @return 手续费对账单
     */
    @ApiOperation("查询手续费对账单列表")
    @GetMapping("/list")
    public TableDataInfo list(RecnSearchForm searchForm) {
        startPage();
        List<RecnVo> listImcRecn = imcRecnService.listImcRecn(searchForm);
        return getDataTable(listImcRecn);
    }

    /**
     * 查询核算结果列表
     *
     * @param recnSearchForm RecnExpenseSearchForm
     * @return 结果
     */
    @ApiOperation("查询核算结果列表")
    @GetMapping("/check/list")
    public TableDataInfo listRecnDetailsList(RecnSearchForm recnSearchForm) {
        startPage();
        List<RecnDetailsCheckVo> recnDetailsCheckVoList = this.imcRecnService.listCheckResultList(recnSearchForm);
        return getDataTable(recnDetailsCheckVoList);
    }

    /**
     * 导出核对结果
     *
     * @param recnSearchForm RecnSearchForm
     * @return 结果
     */
    @ApiOperation("导出核对结果")
    @Log(title = "导出核对结果", businessType = BusinessType.EXPORT)
    @PostMapping("/check/export")
    public AjaxResult exportImcExpenseRecn(@RequestBody RecnSearchForm recnSearchForm) throws NoSuchFieldException, IllegalAccessException {
        List<RecnTemplate> list = imcRecnService.export(recnSearchForm);
        ExcelUtil<RecnTemplate> util = new ExcelUtil<>(RecnTemplate.class);
        return util.exportExcel(list, "手续费核对结果");
    }

    /**
     * 批量处理核对结果
     *
     * @param recnSearchForm RecnSearchForm
     * @return 结果
     */
    @ApiOperation("批量处理核对结果")
    @Log(title = "批量处理核对结果", businessType = BusinessType.UPDATE)
    @PostMapping("/check/dispose")
    public AjaxResult disposeImcRecn(@RequestBody RecnSearchForm recnSearchForm) {
        return imcRecnService.disposeImcRecn(recnSearchForm);
    }

    /**
     * 提交审核
     *
     * @param recnId 主表ID
     * @return 结果
     */
    @ApiOperation("提交审核")
    @Log(title = "提交审核", businessType = BusinessType.UPDATE)
    @GetMapping("/audit/{recnId}")
    public AjaxResult auditImcRecn(@PathVariable("recnId") Long recnId) {
        return imcRecnService.auditImcRecn(recnId);
    }

    /**
     * 审核通过
     *
     * @return 结果
     */
    @ApiOperation("审核通过")
    @Log(title = "审核通过", businessType = BusinessType.UPDATE)
    @PostMapping("/audit/approved/{recnId}")
    public AjaxResult auditApproved(@PathVariable("recnId") Long recnId, @RequestBody RecnSearchForm recnSearchForm) {
        return imcRecnService.auditApproved(recnId, recnSearchForm);
    }

    /**
     * 退回
     *
     * @return 结果
     */
    @ApiOperation("退回")
    @Log(title = "退回", businessType = BusinessType.UPDATE)
    @PostMapping("/audit/return/{recnId}")
    public AjaxResult auditReturn(@PathVariable("recnId") Long recnId, @RequestBody RecnSearchForm recnSearchForm) {
        return imcRecnService.auditReturn(recnId, recnSearchForm);
    }

    /**
     * 反结
     *
     * @return 结果
     */
    @ApiOperation("反结")
    @Log(title = "反结", businessType = BusinessType.UPDATE)
    @PostMapping("/audit/revert/{recnId}")
    public AjaxResult auditRevert(@PathVariable("recnId") Long recnId, @RequestBody RecnSearchForm recnSearchForm) {
        return imcRecnService.auditRevert(recnId, recnSearchForm);
    }

    /**
     * 删除
     */
    @ApiOperation("删除")
    @Log(title = "删除", businessType = BusinessType.UPDATE)
    @GetMapping("/delete/{recnId}")
    public AjaxResult removeImcRecn(@PathVariable("recnId") Long recnId) {
        return imcRecnService.deleteImcRecnById(recnId);
    }
}
